﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

/// <summary>
/// inc 的摘要说明。
/// </summary>
public class inc
{
    private readonly string constr = "data/count.mdb";
    private readonly string ipconstr = "data/address.mdb";
    private OleDbConnection obCon;

    public inc()
    {
        try
        {
            string conSql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(constr);
            obCon = new OleDbConnection(conSql);
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("document.write(\"SQL:" + ex.Message.ToString() + "\");");
        }
    }

    /// <summary>
    /// 执行SQL语句,返回是否执行成功
    /// </summary>
    /// <param name="sql">所执行的SQL语句</param>
    /// <returns></returns>
    public bool ExecBool(string sql)
    {
        try
        {
            obCon.Open();
            OleDbCommand cmd = new OleDbCommand(sql, obCon);
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            obCon.Close();
            System.Web.HttpContext.Current.Response.Write("document.write(\"Ebool:" + sql + ex.Message.ToString() + "\");");
            return false;
        }
        finally
        {
            obCon.Close();
        }

    }

    /// <summary>
    /// 执行SQL语句,返回记录集
    /// </summary>
    /// <param name="sql">所执行的SQL语句</param>
    /// <returns></returns>
    public DataTable Find(string sql)
    {
        try
        {
            obCon.Open();
            OleDbCommand cmd = new OleDbCommand(sql, obCon);
            OleDbDataAdapter db = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            db.Fill(ds);
            return ds.Tables[0];
        }
        catch (Exception ex)
        {
            obCon.Close();
            System.Web.HttpContext.Current.Response.Write("document.write(\"Data:" + ex.Message.ToString() + "\");");
            return null;
        }
        finally
        {
            obCon.Close();
        }
    }


    public string Fn_Dir(string FilePath)
    {
        if (FilePath.Trim() == "")
            return "";
        int abc = 0;
        for (int i = 1; i <= FilePath.Length; i++)
        {
            if (FilePath.Substring(FilePath.Length - i, i).Substring(0, 1) == "/" || FilePath.Substring(FilePath.Length - i, i).Substring(0, 1) == "\\")
            {
                abc = i;
                i = FilePath.Length + 1;
            }
        }
        if (abc != 0)
        {
            return FilePath.Substring(0, FilePath.Length - abc + 1);
        }
        else
            return "";
    }

    public string Fn_IP(string ip)
    {
        try
        {
            if (ip.Trim() == "" || ip.IndexOf(".") <= 0)
                return "";
            ip = ip.ToString();
            string ip1 = ip.Substring(0, ip.IndexOf("."));
            ip = ip.Substring(ip.IndexOf(".") + 1);
            string ip2 = ip.Substring(0, ip.IndexOf("."));
            ip = ip.Substring(ip.IndexOf(".") + 1);
            string ip3 = ip.Substring(0, ip.IndexOf("."));
            string ip4 = ip.Substring(ip.IndexOf(".") + 1);
            int ips = Int32.Parse(ip1) * 256 * 256 * 256 + Int32.Parse(ip2) * 256 * 256 + Int32.Parse(ip3) * 256 + Int32.Parse(ip4);
            return ips.ToString();
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("document.write(\"IP:" + ex.Message.ToString() + "\");");
            return "";
        }
    }

    //转译字符
    public string Fn_EnChar(string body)
    {
        body = body.Replace(Convert.ToString((char)(30)), "&#38;");
        body = body.Replace(">", "&gt;");
        body = body.Replace("<", "&lt;");
        body = body.Replace(Convert.ToString((char)(10)), "<br>");
        body = body.Replace(Convert.ToString((char)(13)), "");

        body = body.Replace(Convert.ToString((char)(34)), "&quot;");
        body = body.Replace(Convert.ToString((char)(39)), "&#39;");
        body = body.Replace(Convert.ToString((char)(32)), "&nbsp;");
        return body;
    }

    //还原字符
    public string Fn_DeChar(string body)
    {
        body = body.Replace("&#38;", Convert.ToString((char)(30)));
        body = body.Replace("&gt;", ">");
        body = body.Replace("<", "&lt;");
        body = body.Replace("<br>", Convert.ToString((char)(10)));
        body = body.Replace("", Convert.ToString((char)(13)));

        body = body.Replace("&quot;", Convert.ToString((char)(34)));
        body = body.Replace("&#39;", Convert.ToString((char)(39)));
        body = body.Replace("&nbsp;", Convert.ToString((char)(32)));
        return body;
    }

    public string GetWhere(string IP)
    {
        if (IP.Trim() == "")
            return "未知";
        string ipwhere = "未知";
        string ipconSql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ipconstr);
        OleDbConnection ipCon = new OleDbConnection(ipconSql);
        ipCon.Open();
        OleDbCommand cmd = new OleDbCommand("select country from address where ip1 <= " + IP + " and ip2 >= " + IP, ipCon);
        OleDbDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            ipwhere = dr["country"].ToString();
        }
        dr.Close();
        ipCon.Close();
        return ipwhere;
    }
}
